let condition = {
  id:'',
  startTime:moment().startOf('day'),
  endTime:moment(),
  websiteId:-1,
  radiodata:[
      {
        label: '倾向性',
        item: ['全部','负面','正面','中性'],
        checked:'全部'
      },{
        label: '监测范围',
        item: ['全部', '全国','本地','境外'],
        checked:'全部'
      },{
        label: '浏览',
        item: ['全部', '已阅', '未阅'],
        checked:'全部',
      },{
        label: '排序',
        item: ['时间降序', '时间升序','相关性排序'],
        checked:'时间降序'
      },{
        label:'相似文章',
        item:['展开','合并'],
        checked:'展开'
      },{
        label:'查看模式',
        item:['列表','摘要'],
        checked:'列表'
      },{
        label:'定向监测',
        full:true,
        item:[],
        checked:'',
        set:true
      }]
}
let loading = false
let infoitems = []
let _infoitems = []//临时存放
let total = 0 //总数
let filters= ['全部','全部','全部','时间降序','展开','列表']
let keywords = ''
let article={} //文章
let simList = [] //相似列表
let xxcount = 0
let loadingthis = false
let types = []

// 监听已阅未阅按钮
let peg = 0
import moment from 'moment'
import Vue from 'vue';
export default {
  state: {
    condition,//所有条件
    id:-1,
    currentPage:1,//当前页
    limit:50,//条数,
    infoitems,//信息内容
    total,//总数
    _infoitems,//临时变量
    keywords,//关键词
    article,//
    simList,//相似文章
    loading,//加载框
    xxcount,
    loadingthis,//加载框
    peg,
    types,
  },
  mutations: {
    UPLIMIT:(state,payload)=>{
      state.limit = payload.limit
    },
    UPDATE:(state,payload)=>{
      state.condition.startTime = payload.startTime
      state.condition.endTime = payload.endTime
    },
    UPWEBSITEID:(state,payload)=>{
      state.condition.websiteId = payload.websiteId
    },
    UPPEG:(state,payload)=>{
      state.peg = payload.peg
    },
    LoadingTrue:(state)=>{
      state.loading = true
      state.loadingthis = true
      function getLoad() {
        if(state.loading){
          setTimeout(getLoad, 2300)
        }else{
          setTimeout(state.loadingthis=false,3000)
        }
      }
      getLoad()
    },
    Loadingfalse:(state)=>{
      state.loading = false
    },
    setxxcount:(state,payload)=>{
      state.xxcount = payload.count
    },
    // 更改条数
    updatelimit:(state,payload)=>{
      let _limit = payload._limit
    },
    // 初次获取
    getScreenings:(state,payload)=>{
        let _index = payload.index
        state.condition.radiodata[_index].checked=payload.value
    },
    // 删除单个
    delScreenings:(state,payload)=>{
      let _index =  payload.index
      state.condition.radiodata[_index].checked = filters[_index]
    },
    // 查询条件
    QUERYCONDITION:(state,r)=>{
      let ch1 = {'负面':2,'正面':1,'中性':0,'全部':-1}//正负面0
      let ch2 = {'全国':2,'本地':1,'境外':3,'全部':-1}//监测范围1
      let ch3 = {'已阅':1,'未阅':0,'全部':-1}//浏览2
      let ch4 = {'poTimedesc':'时间降序','poTimeasc':'时间升序','_scoredesc':'相关性排序'}
      let ch5 = {'合并':1,'展开':0} //相似4
      state.condition.id = r.id
      state.condition.radiodata[0].checked =Object.entries(ch1).filter(([k, v]) => v == parseInt(r.positiveOrNegative))[0][0]//正负面
      state.condition.radiodata[1].checked =Object.entries(ch2).filter(([k, v]) => v == parseInt(r.monitorScope))[0][0]//正负面
      state.condition.radiodata[2].checked =Object.entries(ch3).filter(([k, v]) => v == parseInt(r.readed))[0][0]//正负面
      state.condition.radiodata[4].checked =Object.entries(ch5).filter(([k, v]) => v == parseInt(r.similar))[0][0]//正负面
      state.condition.radiodata[3].checked = ch4[r.sortField+r.sortType]
      state.condition.startTime = state.condition.startTime
      state.condition.endTime = state.condition.endTime
      state.condition.websiteId = r.website
    },
    // 已阅
    yread:(state,payload)=>{
        state.infoitems = state._infoitems
        let id;
        let readArr = []
        let yreadInfo = []//已读数组
        if (localStorage.read) {
            let _readArr = localStorage.read
            readArr = JSON.parse(_readArr);
        }
        state.infoitems.forEach((value,index)=>{
          if(typeof(value.title)!='undefined'){
            id = value.id
          }else{
            id= value.data[0].id
          }
          // 判断是否有已经阅读
          if(readArr.indexOf(id)!=-1){
            yreadInfo.push(value)
          }
        })
        state.infoitems = yreadInfo
    },
    //未阅
    nread:(state,payload)=>{
        state.infoitems = state._infoitems
        let id;
        let readArr = []
        let nreadInfo = []//已读数组
        if (localStorage.read) {
            let _readArr = localStorage.read
            readArr = JSON.parse(_readArr);
        }
        state.infoitems.forEach((value,index)=>{
          if(typeof(value.title)!='undefined'){
            id = value.id
          }else{
            id= value.data[0].id
          }
          // 判断是否有已经阅读
          if(readArr.indexOf(id)==-1){
            nreadInfo.push(value)
          }
        })
        state.infoitems = nreadInfo
    },
  
    // 清空
    deleteAll:(state)=>{
      state.condition.radiodata.forEach((value,index)=>{
        value.checked = filters[index]
      })
      state.condition.websiteId = -1
      state.condition.startTime=moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')
      state.condition.endTime=moment().format('YYYY-MM-DD HH:mm:ss')
    },
    // 信息内容
  },
  actions:{
    // 查询条件
    querycondition({commit},data){
      commit('QUERYCONDITION',data)
    },

    // 清空条件
    deleteAll({commit}){
      commit('deleteAll')
    },
    // 查询条件
    getScreen({commit},data){
      data.that.$http.get('vrv-monitor-platform-web/factor/queryByOrgCode').then((res)=>{
        if (res.body.success==true) {
          commit('qScreenings',res)
        }
      
      },(err)=>{
      })
    },
    // 查信息
    qMessage({commit,state},data){
        commit('LoadingTrue')
        let isFirst =false
        if (typeof(data.first)!='undefined'){
            isFirst = true
        }
        // 页数
        if (typeof(data._currentPage)!='undefined') {
          state.currentPage = data._currentPage
        }
        // 条数
        if (typeof(data._limit)!='undefined') {
          state.limit = data._limit
        }
        let ch1 = {'负面':2,'正面':1,'中性':0,'全部':-1}//正负面0
        let ch2 = {'全部':[-1,-1],'全国':[-1,0],'本地':[1,0],'境外':[-1,1],}//监测范围1
        let ch3 = {'已阅':1,'未阅':0,'全部':-1}//浏览2
        let ch4 = {'时间升序':['poTime','asc'],'时间降序':['poTime','desc'],'相关性排序':['_score','desc']}//时间排序3
        let ch5 = {'合并':'count','展开':'all'} //相似4
        //正负面
        data.that.$http.post('vrv-monitor-platform-web/data/query',{
          isFirst:isFirst,
          topicId:'',
          categoryId:'',
          startTime:moment(state.condition.startTime).format('YYYY-MM-DD HH:mm:ss'),
          endTime:moment(state.condition.endTime).format('YYYY-MM-DD HH:mm:ss'),
          sortType:ch4[state.condition.radiodata[3].checked][1],
          scope:ch2[state.condition.radiodata[1].checked][0],
          abroad:ch2[state.condition.radiodata[1].checked][1],
          websiteType:state.condition.websiteId,
          positiveOrNegative:ch1[state.condition.radiodata[0].checked],
          searchType:ch5[state.condition.radiodata[4].checked],
          currentPage:state.currentPage,
          limit:state.limit,
          sortField:ch4[state.condition.radiodata[3].checked][0]
        }).then(res=>{
            if(res.body.success==true){
              commit('Loadingfalse');
              state.total = res.body.totals
              state.keywords = res.body.keywords//关键词
              state.infoitems = res.body.data
              state._infoitems = res.body.data
              state.infoitems.forEach((value,index)=>{
                data.that.$set(value,'readText', 0 )
                data.that.$set(value,'checked', false )
                data.that.$set(value,'collectName', '加入收藏' )
                if (localStorage.read) {
                      let _readArr = localStorage.read
                      let readArr = JSON.parse(_readArr);
                      let id;
                      if(typeof(value.title)!='undefined'){
                        id=value.id
                      }else{
                        id=value.data[0].id
                      }
                      if(readArr.indexOf(id)!=-1){
                        data.that.$set(value,'isread', true )
                      }else{
                        data.that.$set(value,'isread', false )
                      }  
                  }else{
                    data.that.$set(value,'isread', false )
                  }
                  // 图片
                  let flag = typeof(value.title)!='undefined'
                  if(flag){
                        if(value.collected==false||value.collected=='false'){
                          data.that.$set(value,'enshrine', false )
                        }else{
                          data.that.$set(value,'enshrine', true )
                        }
                      }else{
                        if(value.data[0].collected==false||value.data[0].collected=='false'){
                            data.that.$set(value,'enshrine', false )
                          }else{
                            data.that.$set(value,'enshrine', true )
                        }
                    }
                  let imgurl = flag==true?value.domain:value.data[0].domain
                  data.that.$http.get(`static/${imgurl}.png`).then(res=>{
                    data.that.$set(value,'urlimg', `url(static/${imgurl}.png)` )
                  }).catch(err=>{
                    data.that.$set(value,'urlimg',"url('static/defult.png')")
                  })
                  
              })
              if(state.condition.radiodata[2].checked=='已阅'){

                  commit('yread');
                }else if(state.condition.radiodata[2].checked=='未阅'){
                  commit('nread');
                }else if(state.condition.radiodata[2].checked=='全部'){
                  state.infoitems = state._infoitems
                  // state.total = res.body.totals
                }
                
            }else{
              commit('Loadingfalse')
              data.that.$message.err(res.body.message)
            }
        }).catch(e=>{
          commit('Loadingfalse')
        })
      },
      //查载体类型
      async queryTypes({commit,state},data){
        let isFirst =false
        if (typeof(data.first)!='undefined'){
            isFirst = true
        }
        let ch1 = {'负面':'2','正面':'1','中性':'0','全部':'-1'}//正负面0
        let ch2 = {'全部':[-1,-1],'全国':[-1,0],'本地':[1,0],'境外':[-1,1],}//监测范围1
        let ch3 = {'已阅':1,'未阅':0,'全部':-1}//浏览2
        let ch4 = {'时间升序':'asc','时间降序':'desc',}//时间排序3
        let ch5 = {'合并':'count','展开':'all'} //相似4
        data.that.$http.post('vrv-monitor-platform-web/data/mergequeryTodayOpinionAndqueryTypes',{
          isFirst:isFirst,
          topicId:'',
          categoryId:'',
          startTime:moment(state.condition.startTime).format('YYYY-MM-DD HH:mm:ss'),
          endTime:moment(state.condition.endTime).format('YYYY-MM-DD HH:mm:ss'),
          sortType:ch4[state.condition.radiodata[3].checked],
          scope:ch2[state.condition.radiodata[1].checked][0],
          abroad:ch2[state.condition.radiodata[1].checked][1],
          websiteType:state.condition.websiteId,
          positiveOrNegative:ch1[state.condition.radiodata[0].checked],
          searchType:ch5[state.condition.radiodata[4].checked],
          currentPage:1,
          limit:30,
          aggsField:"webSiteType"
        }).then(res=>{
            if(res.body.success==true){
              let all = res.body.data.reduce((a, d) => a + parseInt(d.doc_count), 0)
            res.body.data.unshift({
              websiteName:'全部',
              doc_count:all,
              websiteId:'-1'
            })
            state.types = res.body.data
            }
        })
      },
      // 文章查询
      queryarticle:({commit,state},data)=>{
          let readArr = []
          let id = data.id
          let module_id = data.module_id
          localStorage.module_id = module_id

          if (localStorage.read) {
            let _readArr = localStorage.read
            readArr = JSON.parse(_readArr);
          }
          if (readArr.indexOf(id)==-1) {
            readArr.push(id)
          }
          let _readArr1 = JSON.stringify(readArr); 
          localStorage.read = _readArr1
          
          let str = JSON.stringify(data); 
          localStorage.obj = str
          window.open('#/details');
          commit('Loadingfalse')
      },
  }
}
